package fr.esipe.eioc3.timetableontime_v2;

import java.util.Calendar;



import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	public static final boolean TASK_MODE_DIALOG_ENABLED = true;
	public static final boolean SERVICE_MODE_DIALOG_DISABLED = false;
	public static final boolean NEED_A_DISPLAY = true;

	AutoRefreshManager refreshManager;
	InitManager initManager;

	public final Context context=this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		refreshManager = new AutoRefreshManager(this);
		initManager = new InitManager(this);

		/* All Buttons of Main Activity */

		Button timeTableDisplayButton = (Button) findViewById(R.id.timetable_display_button);
		Button weekSelectionButton = (Button) findViewById(R.id.week_selection_button);


		/* When timetable_display_button is clicked */

		timeTableDisplayButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				Intent i = new Intent(context, TimeTableDisplay.class);
				MainActivity.this.startActivity(i);
			}
		});


		/* When week_selection_button is clicked */

		weekSelectionButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				Intent i = new Intent(context, SelectWeek.class);
				MainActivity.this.startActivity(i);				
			}
		});

	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		
		
		return super.onTouchEvent(event);
	}

	@Override
	protected void onStart() {
		super.onStart();
		
		//== INITIALISATION au premier lancement de l'application
		initManager.checkForInit();
		
		// == Verification si la formation est bien selectionnee
		initManager.checkForFormationSelected(); 

		//== Verification si la formation a change pour telecharger le nouvelle emploi du temps
		initManager.checkForFormationChange();
			
		//== Verification si autoRefresh a ete active dans les preferences
		refreshManager.checkForAutoRefresh();

		/**
		 * == AFFICHAGE DU PROCHAIN COURS
		 */					
		SharedPreferences mypref = PreferenceManager.getDefaultSharedPreferences(context);
		TextView displayTextView = (TextView) findViewById(R.id.calendar_display);

		Calendar myCalendar = Calendar.getInstance();	

		if(myCalendar.get(Calendar.HOUR_OF_DAY) > 18 || myCalendar.get(Calendar.HOUR_OF_DAY) < 8){
			myCalendar.set(Calendar.HOUR_OF_DAY, 8);
			if(myCalendar.get(Calendar.HOUR_OF_DAY) > 18){
				myCalendar.set(Calendar.DAY_OF_MONTH, myCalendar.get(Calendar.DAY_OF_MONTH)+1);
			}
		}
		if(myCalendar.get(Calendar.HOUR_OF_DAY) == 9 || myCalendar.get(Calendar.HOUR_OF_DAY) == 11 ){
			myCalendar.set(Calendar.HOUR_OF_DAY, myCalendar.get(Calendar.HOUR_OF_DAY)+1);
		}
		if(myCalendar.get(Calendar.HOUR_OF_DAY) == 14){
			myCalendar.set(Calendar.HOUR_OF_DAY, myCalendar.get(Calendar.HOUR_OF_DAY)+2);
		}
		Log.d("Main activity","prochain cours: le "+myCalendar.get(Calendar.DAY_OF_MONTH)+" a "+	myCalendar.get(Calendar.HOUR_OF_DAY)+"h");

		//Recuperation du prochain cours dans la database
		EventProvider provider = new EventProvider(context);
		Cursor cursor = provider.getSpecificEvent(myCalendar.get(Calendar.DAY_OF_MONTH), myCalendar.get(Calendar.HOUR_OF_DAY));

		if(cursor.moveToFirst()){
			Log.d("EventProvider","Get the specific event: "+cursor.getString(cursor.getColumnIndex(EventOpenHelper.EVENT_COLUMN_LABEL0)));

			displayTextView.setText( "== "+mypref.getString("_formationSelectionKey", "unknown")+" =="
									+"\n"
									+"PROCHAIN COURS :"+"\n"
        							+ "----------------"+"\n"
    				+ cursor.getInt(cursor.getColumnIndex(EventOpenHelper.EVENT_COLUMN_STARTHOUR)) +"h"
    				+ cursor.getString(cursor.getColumnIndex(EventOpenHelper.EVENT_COLUMN_STARTMINUTE)) +"\n"
    				+ cursor.getString(cursor.getColumnIndex(EventOpenHelper.EVENT_COLUMN_LABEL0)) +"\n"
        			+ cursor.getString(cursor.getColumnIndex(EventOpenHelper.EVENT_COLUMN_LABEL1)) +"\n"		
        			+ cursor.getString(cursor.getColumnIndex(EventOpenHelper.EVENT_COLUMN_LABEL2)) +"\n");

		}

		provider.close();
		cursor.close();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
			case R.id.menu_settings:
				Log.d("Main activity","click setting button");
				this.startActivity(new Intent(this, SettingActivity.class));
				break;
			case R.id.refresh_button:
				Log.d("Main activity","click refresh button");
				if(new NetworkManager(context).checkIfNetworkIsAvailable(R.string.network_error)){
					//Lancement du telechargement de l'emploi du temps 
					RefreshTask task = new RefreshTask(this,TASK_MODE_DIALOG_ENABLED);
					task.execute();
				}
				break;
			default:break;
		}
		return super.onOptionsItemSelected(item);

	}

}
